package com.lovo.csc.controller;

import com.lovo.commom.util.ConstantUtil;
import org.springframework.stereotype.Service;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;

@Service
@ServerEndpoint("/webSocket")
public class WsController {
    Session session;

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        System.out.println("开始");
    }

    /**
     * 链接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        System.out.println("关闭连接");
    }

    /**
     * 收到客户端消息后调用的方法
     *
     * @param message
     */
    @OnMessage
    public void onMessage(String message) throws IOException, InterruptedException, EncodeException {
        System.out.println(message);
        while (true) {
            if (ConstantUtil.WS_MSG.size()!=0) {
                String poll = ConstantUtil.WS_MSG.poll();
                session.getBasicRemote().sendText(poll);
            }
        }
    }


}
